package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/utils"
)

func Userywdadd1(ctx *gin.Context) {

	var weixins []models.YwWeixin
	data_source.Db.Debug().Limit(20000).Offset(0).Order("id asc").Find(&weixins)
	//data_source.Db.Debug().Where("mobile = 13524918667").Order("id asc").Find(&weixins)
	var i int64
	for _, v := range weixins {
		fmt.Println(i)
		i = i + 1
		var user models.User
		data_source.Db.Where("unionid = ?", v.Unionid).First(&user)

		areaarr := strings.Split(v.Area, ",")
		typearr := strings.Split(v.Types, ",")
		var classifys []models.Classify
		var classifyids []string
		if len(typearr) > 0 {
			data_source.Db.Where("value in (?)", typearr).Find(&classifys)
			for _, c := range classifys {
				classifyids = append(classifyids, utils.Int64string(c.ID))
			}
		}
		yearareaarr := strings.Split(v.YearArea, ",")
		yeartypearr := strings.Split(v.YearTypes, ",")

		var classifys1 []models.Classify
		var classifyids1 []string
		if len(yeartypearr) > 0 {
			data_source.Db.Where("value in (?)", yeartypearr).Find(&classifys1)
			for _, c := range classifys1 {
				classifyids1 = append(classifyids1, utils.Int64string(c.ID))
			}
		}
		var uid int64
		if user.ID == 0 {
			adduser := models.User{
				LoginTime:       int64(v.SubscribeTime),
				Ywdgzheopenid:   v.Openid,
				CreateTime:      v.RegTime,
				LoginNum:        1,
				Logo:            v.Headimgurl,
				Nickname:        v.Nickname,
				Status:          1,
				Unionid:         v.Unionid,
				Phone:           v.Mobile,
				Price:           v.Money,
				Guanlian:        2,
				Identity:        1,
				Vip:             int64(v.Vip) + 1,
				Vipendtime:      v.VipEnd,
				State:           1,
				Laiyuan:         2,
				Classifyids:     strings.Join(classifyids, ","),
				Provincecodes:   v.Area,
				Iflogin:         1,
				Bninttime:       v.YearStart,
				Bnendtime:       v.YearVip,
				Bnclassifyids:   strings.Join(classifyids1, ","),
				Bnprovincecodes: v.YearArea,
			}
			data_source.Db.Create(&adduser)
			uid = adduser.ID

		} else {
			adduser := models.User{
				CreateTime:      v.RegTime,
				Classifyids:     strings.Join(classifyids, ","),
				Provincecodes:   v.Area,
				Vipendtime:      v.VipEnd,
				Bninttime:       v.YearStart,
				Bnendtime:       v.YearVip,
				Bnclassifyids:   strings.Join(classifyids1, ","),
				Bnprovincecodes: v.YearArea,
			}
			data_source.Db.Where("id = ?", user.ID).Updates(&adduser)
			uid = user.ID
		}

		if len(areaarr) > 0 && len(typearr) > 0 {
			data_source.Db.Where("uid = ?", uid).Where("state = 1").Delete(&models.Tuisongshezhi{})
			var tuisongshezhis []models.Tuisongshezhi
			for _, p := range areaarr {
				for _, c := range classifys {
					tuisongshezhi := models.Tuisongshezhi{
						Uid:        uid,
						Provinceid: utils.StrToInt64(p),
						Classifyid: c.ID,
						State:      1,
					}
					tuisongshezhis = append(tuisongshezhis, tuisongshezhi)
				}
			}
			data_source.Db.Create(&tuisongshezhis)
		}
		if len(yearareaarr) > 0 && len(yeartypearr) > 0 {
			data_source.Db.Where("uid = ?", uid).Delete(&models.Baonian{})
			var baonians []models.Baonian
			for _, p := range yearareaarr {
				for _, c := range classifys1 {
					baonian := models.Baonian{
						Uid:        uid,
						Provinceid: utils.StrToInt64(p),
						Classifyid: c.ID,
					}
					baonians = append(baonians, baonian)
				}
			}
			data_source.Db.Create(&baonians)
		}
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"error":   1,
		"success": 0,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
